<?php class inventory extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function insert($data){
       $result = $this->db->insert('inventory',$data);
       return $result;
    }
    
    function select_join($ar){
       $this->db->select('*');
       $this->db->where($ar);
       $this->db->where('count !=',0);
	   $this->db->from('inventory');
	   $this->db->join('category', 'inventory.category_id = category.category_id');
       $query = $this->db->get();
       $result = $query->result(); 
       return $result;
    
    }
    
    function getlast($version_id,$id){
       $this->db->where(array('category_id'=>$id,'version_id'=>$version_id));
       $this->db->select_sum('count');
       $query = $this->db->get('inventory');
       $result = $query->result(); 
       return $result;
    }
    
    
    function select_by_id($inventory_id){
       $this->db->where('inventory_id',$inventory_id);
       $query = $this->db->get('inventory');
       $result = $query->result(); 
       return $result;
    }
    function getlast_fo($version_id,$id){

       $this->db->where('category_id',$id);
       $this->db->where('version_id !=',$version_id);
       $this->db->select_sum('count');
       $query = $this->db->get('inventory');
       $result = $query->result(); 
       return $result;
    }
    
    function getlast_foo($version_id,$id){

       $this->db->where('category_id',$id);
       $this->db->where('version_id !=',$version_id);
       $this->db->where('count !=',0);
       $query = $this->db->get('inventory');
       $result = $query->result(); 
       return $result;
    }
    
    
    function getcount($id,$version_id=''){
    	$where['category_id']=$id;
    	if($version_id!=""){
    	 $where['version_id']=$version_id;
    	}
        $this->db->where($where);
    	$this->db->select_sum('count');
        $query = $this->db->get('inventory');
        $result = $query->result(); 
        return $result;
    }
    
    function select_count($category_id){
       $this->db->select_sum('count');
       $this->db->where('category_id',$category_id);
       $query = $this->db->get('inventory');       
       $result = $query->result(); 

       return $result;    
    }
    
    
    function select_where($category_id,$version_id){
       $this->db->select_sum('count');
       $this->db->where('category_id',$category_id);
       $this->db->where('version_id !=',$version_id);
       $query = $this->db->get('inventory');
       
       $result = $query->result(); 

       return $result[0]->count;
    }
    
    
    function  select_by_array($array){
       $this->db->where($array); 	
       $query = $this->db->get('inventory');       
       $result = $query->result(); 
       return $result;
    }
    
    
    function select($array,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($array!=""){
       	  $this->db->where($array);
       }
       $this->db->where('count !=',0);
       $this->db->order_by('inventory_id desc');
       $query = $this->db->get('inventory');
       
       $result = $query->result(); 
       return $result;
    }
    
    
    function select2($array,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($array!=""){
       	  $this->db->where($array);
       }
       $this->db->order_by('inventory_id desc');
       $query = $this->db->get('inventory');
       
       $result = $query->result(); 
       return $result;
    }

    
    function update($id,$array){
       $result = $this->db->update('inventory',$array,array('inventory_id'=>$id));
       return $result;
    }
    
    function del($id){
       $result = $this->db->delete('inventory',array('inventory_id'=>$id));
       return $result;
    }
}